/********************************************************************************
* @file    LimitSwitch.h
* @author  
* @version 
* @date    
* @brief   This file contains all functions prototype and macros for the sensors peripheral.
******************************************************************************
* @attention
* <h2><center>&copy; COPYRIGHT 2012 mitianshenyu</center></h2>
*******************************************************************************/

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef	_H_LIMITSWITCH_FILE_
#define	_H_LIMITSWITCH_FILE_

/* Includes ------------------------------------------------------------------*/
//#include "stm8s.h"

/* Configurable parameters-------------------------------------------------------*/

/* Exported constand ------------------------------------------------------------*/
#define LIMIT_SWITCH_NUMBER             6

/* Exported types ------------------------------------------------------------*/

//typedef _Bool @far (*CBCheckSpotCritical)(u16);

enum
{
    LIMIT_SWITCH_LEFT   = (u8)0,
    LIMIT_SWITCH_XMID   = (u8)1,
    LIMIT_SWITCH_RIGHT  = (u8)2,
    LIMIT_SWITCH_BOT    = (u8)3,
    LIMIT_SWITCH_YMID   = (u8)4,
    LIMIT_SWITCH_TOP    = (u8)5    
};

typedef struct _LimitState
{
    u8 left : 1;
    u8 xmid : 1;
    u8 right: 1;
    u8 bot  : 1;
    u8 ymid : 1;
    u8 top  : 1;
}LIMITSTATE;

/**  Limit Port define */
#define LIMIT_SWITCH_PORTS              GPIOD

#define LIMIT_SWITCH_LEFT_PORT          GPIOD
#define LIMIT_SWITCH_LEFT_POS           6
#define LIMIT_SWITCH_LEFT_MASK          BIT(LIMIT_SWITCH_LEFT_POS)

#define LIMIT_SWITCH_XMID_PORT          GPIOD
#define LIMIT_SWITCH_XMID_POS           5
#define LIMIT_SWITCH_XMID_MASK          BIT(LIMIT_SWITCH_XMID_POS)

#define LIMIT_SWITCH_RIGHT_PORT         GPIOD
#define LIMIT_SWITCH_RIGHT_POS          4
#define LIMIT_SWITCH_RIGHT_MASK         BIT(LIMIT_SWITCH_RIGHT_POS)

#define LIMIT_SWITCH_BOT_PORT           GPIOD
#define LIMIT_SWITCH_BOT_POS            3
#define LIMIT_SWITCH_BOT_MASK           BIT(LIMIT_SWITCH_BOT_POS)

#define LIMIT_SWITCH_YMID_PORT          GPIOD
#define LIMIT_SWITCH_YMID_POS           2
#define LIMIT_SWITCH_YMID_MASK          BIT(LIMIT_SWITCH_YMID_POS)

#define LIMIT_SWITCH_TOP_PORT           GPIOD
#define LIMIT_SWITCH_TOP_POS            0
#define LIMIT_SWITCH_TOP_MASK           BIT(LIMIT_SWITCH_TOP_POS)

/* Exported macro functions---------------------------------------------------*/
#define LIMIT_SWITCH_EXTI                       (EXTI->CR1)
#define LIMIT_SWITCH_EXTI_LSB                   (6)
#define LIMIT_SWITCH_EXTI_MASK                  ((u8)0B0000000011 << LIMIT_SWITCH_EXTI_LSB)
#define LIMIT_SWITCH_EXTI_MODE_LOW_FALLING      ((u8)0 << LIMIT_SWITCH_EXTI_LSB)                      
#define LIMIT_SWITCH_EXTI_MODE_RISING           ((u8)1 << LIMIT_SWITCH_EXTI_LSB)
#define LIMIT_SWITCH_EXTI_MODE_FALLING          ((u8)2 << LIMIT_SWITCH_EXTI_LSB)
#define LIMIT_SWITCH_EXTI_MODE_RISING_FALLING   ((u8)3 << LIMIT_SWITCH_EXTI_LSB)

#define LIMIT_SWITCH_EXTI_PRIORITY              (ITC->ISPR2)          
#define LIMIT_SWITCH_EXTI_PRIORITY_LSB          (4)
#define SetLimitSwitchEXTIPriority(priority)    {LIMIT_SWITCH_EXTI_PRIORITY |= ((u8)priority) << LIMIT_SWITCH_EXTI_PRIORITY_LSB;}

/* Exported variable --------------------------------------------------------*/
extern IOMASK  LimitSwitch[LIMIT_SWITCH_NUMBER];
extern LIMITSTATE LimitState;
/* Exported functions --------------------------------------------------------*/
void InitLimitSwitch(void);
void LimitSwitchHandle(void);

#endif/* END LIMITSWITCH */
